package com.qire.manhua.controller;

import android.content.Intent;
import android.view.View;
import android.widget.ViewSwitcher;

import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.DialogFastBuildUtil;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.activity.ComicDownloadProgressActivity;
import com.qire.manhua.databinding.ActivityComicDownloadProgressBinding;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.impl.ComicDownloadProgressViewModel;
import com.qire.manhua.model.viewModel.impl.ComicDownloadProgressViewModel.DownloadProgressItemViewModel;
import com.qire.other.recorder.ComicActionRecorder;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

public class ComicDownloadProgressController extends EventController<ComicDownloadProgressActivity> implements AutoVariable {

    private ComicDownloadProgressViewModel viewModel;

    public ComicDownloadProgressController(@NonNull ComicDownloadProgressActivity lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        ActivityComicDownloadProgressBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setComicDownloadProgressController(this);
    }

    @Override
    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.comicDownloadProgressController, this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ComicDownloadProgressViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void backBtnOnClick(View view) {
        owner.onBackPressed();
    }

    public void toggleEditModeOnClick(View view) {
        ViewSwitcher viewSwitcher = ObjectUtil.as(view, ViewSwitcher.class);
        if(ObjectUtil.nonNull(viewSwitcher)) {
            viewSwitcher.showNext();
        }
        viewModel.toggleEditMode();
    }

    public void toggleSortModeOnClick(View view) {
        ViewSwitcher viewSwitcher = ObjectUtil.as(view, ViewSwitcher.class);
        if(ObjectUtil.nonNull(viewSwitcher)) {
            viewSwitcher.showNext();
        }
        viewModel.toggleSortMode();
    }

    public void downloadMoreBtnOnClick(View view) {
        ComicBookEntity comicBookEntity = viewModel.comicBookEntity();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicDownloadSelectorPage)
                .withData("comicBookEntity", comicBookEntity)
                .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .navigation();

        // 行为数据统计
        ComicActionRecorder.build(ComicActionRecorder.count_down)
                .setSource("书架（下载更多）")
                .setComicInfo(comicBookEntity.id, comicBookEntity.bookName)
                .doRecorde(view.getContext());
    }

    private volatile boolean isBatchRunning = false;
    public void allBatchOperationBtnOnClick(View view) {
        if(isBatchRunning) {
            return;
        }
        isBatchRunning = true;
        viewModel.batchToggleDownloadStatus();
        isBatchRunning = false;
    }

    public void allSelectBtnOnClick(View view) {
        viewModel.toggleAllSelect();
    }

    public void deleteSelectedBtnOnClick(final View view) {

        if(viewModel.noSelect()){
            ToastHelper.showToast("请先选择删除项");
            return ;
        }

        DialogFastBuildUtil.builder(R.layout.dialog_common_prompt_light_l)
                .setTextView(R.id.dialog_title, "删除提示")
                .setTextView(R.id.dialog_content, "是否删除已选中章节？")
                .setTextView(R.id.dialog_button_light, "取消", cancelView -> DialogFastBuildUtil.dismissDialogBy(cancelView))
                .setTextView(R.id.dialog_button_dark, "确定", confirmView -> {
                    viewModel.batchDelDownloadRecord();
//                    viewModel.toggleEditMode();
                    DialogFastBuildUtil.dismissDialogBy(confirmView);
                })
                .show();

    }

    /***--------------------------------章节下载进度项事件---------------------------*/

    public void chapterDownloadProgressItemOnClick(View view) {
        if(viewModel.isEditMode()) {
            selectButtonOnClick(view);
        } else {
            controllerButtonOnClick(view);
        }
    }

    public void selectButtonOnClick(View view) {
        DownloadProgressItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), DownloadProgressItemViewModel.class);
        if(ObjectUtil.nonNull(itemViewModel)) {
            itemViewModel.toggleSelect();
        }
    }

    public void controllerButtonOnClick(View view) {
        DownloadProgressItemViewModel itemViewModel = ObjectUtil.as(view.getTag(), DownloadProgressItemViewModel.class);
        if(ObjectUtil.isNull(itemViewModel)) {
            return;
        }

        if(itemViewModel.isDownloadComplete()) {
            ComicBookEntity comicBookEntity = viewModel.comicBookEntity();
            comicBookEntity.chaptersReading(itemViewModel.chapter.getId());
            AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.ComicReaderPage)
                    .withData("comicBookEntity", comicBookEntity)
                    .navigation();
            return;
        }

        itemViewModel.toggleDownloadStatus();
    }

    /***--------------------------------章节下载进度项事件---------------------------*/


}
